#!/usr/bin/env bash

# HSR patch by mkrsym1
# https://notabug.org/mkrsym1/astra
# mirror: https://codeberg.org/an-anime-team/astra

script_dir=`dirname "$0"`
source "${script_dir}"/common

error "PROVEN UNSAFE. MONITOR #7 ON THE MAIN REPOSITORY."

if [ "$(realpath "${script_dir}")" == "$(realpath .)" ]; then
    error "Do not put the patch installer into the game directory!"
fi

if ! command -v xdelta3 2>&1 >> /dev/null; then
    error "xdelta3 is required for patching the game files, but was not found!"
fi

patch_data=""
root_cmd="pkexec"
yes_to_all=""
no_root=""
# Parse commandline arguments
while [ "x$1" != "x" ];
do
    case $1 in
        --patch-data)
            shift
            patch_data="$1"
            ;;
        --root-cmd)
            shift
            root_cmd="$1"
            ;;
        --yes-to-all)
            yes_to_all="1"
            ;;
        --no-root)
            no_root="1"
            ;;
        *)
            error "Invalid commandline argmument: $1"
            ;;
    esac
    shift
done

if [ "x${patch_data}" == "x" ]; then
    error "Patch data directory not specified!"
fi

# Logging servers
logging_servers_os=$(cat <<EOF
# Honkai Star Rail logging servers (oversea)
0.0.0.0 log-upload-os.hoyoverse.com
0.0.0.0 sg-public-data-api.hoyoverse.com
EOF
)


# Region detection
game_region=""
logging_servers=""

starrailbase_checksum=($(md5sum StarRailBase.dll))
unityplayer_checksum=($(md5sum UnityPlayer.dll))
case "${starrailbase_checksum}-${unityplayer_checksum}" in
    "8aa3790aafa3dd176678392f3f93f435-f17b9b7f9b8c9cbd211bdff7771a80c2")
        game_region="os"
        logging_servers="${logging_servers_os}"
        ;;
    *)
        error "Wrong game version or game is already patched. Run uninstall.sh to remove the patch."
        ;;
esac

info "Detected region: ${game_region}"

warn "VERSION WITH EXPERIMENTAL MITIGATIONS. MIGHT NOT WORK."

if ! [ "${yes_to_all}" ]; then
    echo
    echo "Modifying the game is a violation of it's Terms of Service! Make sure that you understand all of the risks and possible consequences before proceeding!"
    read -p "Do you wish to proceed? [y/N] " response
    if [[ ! "$response" == [Yy]* ]]; then
        exit 1
    fi
fi


echo
info "Installing the patch"

mkdir -p "${patch_data}"

echo
if ! [ "${no_root}" ]; then
    if [[ ! `cat /etc/hosts` == *"$logging_servers"* ]]; then
        info "Blocking logging servers. This will require superuser privileges (canceling will skip this step)"
        echo "$logging_servers" | $root_cmd tee -a /etc/hosts 2>&1 >> /dev/null
        if test $? -ne 0; then
            warn "Could not block logging servers. Please add the following lines to your /etc/hosts manually:"
            echo "$logging_servers"
        fi
    else
        info "Logging servers are already blocked"
    fi
else
    info "Skipped blocking logging servers because of --no-root"
fi

echo
info "Copying additional files"
for additional_file in ${additional_files[@]}
do
    info "Copying ${additional_file}"
    cp -r "${script_dir}/${game_region}/files/${additional_file}" "${patch_data}"
    if test $? -ne 0; then
        error "Failed to copy ${additional_file}!"
    fi
done


info "Creating the game launch script"
cat "${script_dir}/${game_region}/files/launch1.bat" > ./launch.bat
winepath -w "${patch_data}/TVMBootstrap.dll" | sed 's|\\|\\\\|g' | tr -d "\n" >> ./launch.bat
cat "${script_dir}/${game_region}/files/launch2.bat" >> ./launch.bat


echo
info "Patching game files"
for game_file in ${game_files[@]}
do
    if test -f "${game_file}"; then
        info "Patching ${game_file}"
        
        mv "${game_file}" "${game_file}.bak"
        xdelta3 -d -s "${game_file}.bak" "${script_dir}/${game_region}/diffs/${game_file}.vcdiff" "${game_file}"
        if test $? -ne 0; then
            error "Failed to patch ${game_file}!"
        fi
    else
        error "${game_file} not found!"
    fi
done

echo
info "Done"
echo
echo "!!! PLEASE DON'T SHARE THIS PROJECT IN PUBLIC !!!"

echo
